﻿using RSTAi.Platform.GrpcService.Contexts;
using RSTAi.Platform.GrpcService.Models;
using Grpc.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using RSTAi.Platform.GrpcService.Common;
using Microsoft.AspNetCore.Authorization;
using System.Text;

namespace RSTAi.Platform.GrpcService.Services
{

    public class JwtTokenServices : IJwtTokenServices.IJwtTokenServicesBase
    {
        readonly MyDBContext _dBContext;
        readonly ILogger<JwtTokenServices> _logger;
        public JwtTokenServices(MyDBContext dBContext, ILogger<JwtTokenServices> logger)
        {
            this._dBContext = dBContext;
            _logger = logger;
        }

        /// <summary>
        /// 获取token
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task<ResponeDto> GetToken(TokenFrom request, ServerCallContext context)
        {
            //测试客户端 
            //var t = _scannerServiceClient.GetPointCloudData(scannerBufferRequest);
            ResponeDto responeDto = new ResponeDto();
            try
            {
                bool ret = true;
                string sign = string.Empty;
                await Task.Run(() =>
                {
                    if (string.IsNullOrWhiteSpace(request.StationId))
                    {
                        ret = false;
                        responeDto.Message = "StationId  cannot be empty!";
                    }
                    if (string.IsNullOrWhiteSpace(request.TimeStamp))
                    {
                        ret = false;
                        responeDto.Message = "TimeStamp  cannot be empty!";
                    }
                    if (string.IsNullOrWhiteSpace(request.Sign))
                    {
                        ret = false;
                        responeDto.Message = "Sign  cannot be empty!";
                    }
                    if (ret)
                    {
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.Append(request.StationId);
                        stringBuilder.Append(request.TimeStamp);
                        stringBuilder.Append(MD5Helper.Md5Key);
                        sign = MD5Helper.GetMD5(stringBuilder.ToString());
                    }

                });
                if (!ret)
                {
                    responeDto.IsSuccess = false;
                    responeDto.Code = 101;
                    return responeDto;
                }

                if (request.Sign != sign)
                {
                    responeDto.IsSuccess = false;
                    responeDto.Code = 101;
                    responeDto.Message = "Sign verification failed!";
                    return responeDto;
                }
                else
                {
                    responeDto.IsSuccess = true;
                    responeDto.Code = 100;
                    string token = TokenHelper.CreateToken(request.StationId);
                    responeDto.Token = token;
                    CacheHelper.SetCache(token, token);
                    return responeDto;
                }
            }
            catch (Exception ex)
            {
                responeDto.IsSuccess = false;
                responeDto.Message = ex.Message;
                responeDto.Code = 999;
                _logger.LogError(ex.Message, "GetToken");
            }

            return responeDto;
        }





    }
}
